//
//  BaseListViewController.m
//  JJHW_coreData
//
//  Created by lizhenqiang on 16/4/8.
//  Copyright © 2016年 HFKJ_LZQ. All rights reserved.
//

#import "BaseListViewController.h"
#import "MJRefresh.h"
@interface BaseListViewController ()<UIScrollViewDelegate>
{
    UIScrollView *_dataScrollView;
    NSInteger _totalPages;
}
@end

@implementation BaseListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.currentPage =0;
    self.pageDataArr =[NSMutableArray new];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setTotalPages:(NSInteger)totalPage
{
    _totalPages =totalPage;
}
#pragma mark - Refresh Control And Request Latest Data

- (void)addRefreshHeader:(UIScrollView *)scrollView
{
    _dataScrollView =scrollView;
    //    _dataScrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    //
    //        [scrollView.mj_header endRefreshing];
    //
    //    }];
    _dataScrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    [_dataScrollView.mj_header beginRefreshing];

    
}
- (void)addRefreshHeaderAndFooterView:(UIScrollView *)scrollView
{
    _dataScrollView =scrollView;
    _dataScrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    _dataScrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
    [_dataScrollView.mj_header beginRefreshing];
    
}
//- (void)endRefreshControlLoading
//{
//        if (self.currentPage ==0)
//        {
//            [_dataScrollView.mj_header endRefreshing];
//        }
//        else {
//            [_dataScrollView.mj_footer endRefreshing];
//        }
//}
#pragma mark -override
- (void)loadDataWithCurrentpage:(NSInteger)currentpage
{
    
}
- (void)loadAllDataWithOtherVC
{
    
}
- (void)headerRereshing
{
    [self resetFirstLoadData];
    [self loadDataWithCurrentpage:self.currentPage];
}
- (void)footerRereshing
{
    if (self.currentPage < _totalPages)
    {
        self.currentPage++;
        [self loadDataWithCurrentpage:self.currentPage];
    } else
    {
        if (self.isDefault)
        {
            [_dataScrollView.mj_footer endRefreshing];
            [self loadAllDataWithOtherVC];
        }
        else
        {
//            [_dataScrollView.mj_header endRefreshing];
            [_dataScrollView.mj_footer endRefreshing];
            [self.view makeToast:@"没有更多的数据" duration:1.0 position:@"center"];
        }
    }
    
}
- (void)endRefreshControlLoading
{
        if (self.currentPage ==0)
        {
            [_dataScrollView.mj_header endRefreshing];
        }
        else {
            [_dataScrollView.mj_footer endRefreshing];
        }
}

- (void)resetFirstLoadData
{
    self.currentPage =0;
    [self.pageDataArr removeAllObjects];
    [(UITableView *)_dataScrollView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
